<template>
  <div class="ref">
    <form action="/">
      <van-search placeholder="" v-model="sval" show-action @search="onSearch" @cancel="onCancel" style="position:fixed;top:0vh;left:0vw;width:100vw;z-index:1" />
    </form>
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh" style="margin-top:8vh;margin-bottom:8vh;">
      <van-list v-model="loading" :finished="finished" finished-text="已加载全部数据" @load="onLoad">
        <van-cell v-for="item in list" :key="item.id" :value="item.value" @click="reBack(item)">
          <template #title>
            <span>{{ item.title }}</span><br>
            <span style="font-size: 12px;color: #969799;" v-html="item.label"></span>
          </template>
        </van-cell>
      </van-list>
    </van-pull-refresh>
    <van-tabbar v-if="this.$route.params.tobo == 'bld'">
      <van-tabbar-item @click="openNew">
        <van-button type="info" style="width:25vw">新增</van-button>
      </van-tabbar-item>
    </van-tabbar>
  </div>
</template>

<script>
import Vue from 'vue'
import VueKeyCloak from '@dsb-norge/vue-keycloak-js'
import wx from 'weixin-js-sdk'
// import * as ww from 'https://open.work.weixin.qq.com/wwopen/js/jwxwork-1.0.0.js'
import { Search, List, Cell, PullRefresh, Toast, Tabbar, TabbarItem, Popup } from 'vant'
import qs from 'qs'
Vue.use(Search).use(List).use(Cell).use(PullRefresh).use(Toast).use(Tabbar).use(TabbarItem).use(Popup)

export default {
  name: 'ref',
  data () {
    return {
      sval: this.$route.params.conditionVal,
      list: [],
      loading: false,
      finished: false,
      isLoading: false,
      startline: 1,
      rebackobject: { reBackflag: false },
      fieldref: this.$route.params.fieldref,
      'ref_sourceid': this.$route.params.sourceId,
      'ref_sourcetemplate': this.$route.params.sourceTemplate,
      wd: 28.152734,
      jd: 113.614169
    }
  },
  mounted () {
    // const script = document.createElement('script')
    // script.src = 'https://open.work.weixin.qq.com/wwopen/js/jwxwork-1.0.0.js'
    // script.onload = () => {
    //   //  获取地址
    //   this.getlocation()
    // }
    // document.body.appendChild(script)
  },
  methods: {
    onLoad () {
      let data = {
        'ref_object': this.$route.params.bo,
        'ref_toobject': this.$route.params.tobo,
        'ref_tofieldas': this.$route.params.field,
        'ref_inview': this.$route.params.conditionValHidden,
        'view_page': this.startline,
        'view_id': this.$route.params.viewid,
        'ref_classname': this.$route.params.classname,
        'ref_params': this.$route.params.params
      }
      //console.log('data:', data)
      //console.log(this.sval)
      data.ref_inview = data.ref_inview.replace(':in:', ':in:' + this.sval)
      //console.log(data.ref_inview)
      let url = '/api/ref.listnew.do'
      let projectData = require('@/project.json')
      if (projectData.project && projectData.project == 'dinuo/') {
        let configData = require('@/json/dinuoconfig.json')
        let refObj = this.$route.params.tobo

        // if (refObj == 'bld' && this.$route.params.bo == 'account') {
        //   url = '/api/ref.listbldaccount.do'
        //   data.jd = this.jd
        //   data.wd = this.wd
        // }
      }

      this.axios({
        method: 'post',
        url: url,
        data: qs.stringify(data)
      }).then(res => {
        //console.log(res)
        if (res.data.flag === '1') {
          Toast(res.data.des)
          if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
            this.loginout()
          } else {
            this.$router.go(-1)
          }
        } else {
          let rsdata = res.data.jsondata.outdata
          for (let i = 0; i < rsdata.length; i++) {
            this.list.push(rsdata[i])
          }
          if (rsdata == null || rsdata.length === 0) {
            this.loading = false
            this.finished = true
          } else {
            this.startline = this.startline + 1
            this.loading = false
          }
        }
      }).catch(res => {
        this.reserror(res)
      })
    },
    onRefresh () {
      this.list = []
      this.startline = 1
      this.isLoading = false
      this.loading = true
      this.finished = false
      this.onLoad()
    },
    openNew () {
      let projectData = require('@/project.json')
      if (projectData.project && projectData.project == 'dinuo/') {
        let configData = require('@/json/dinuoconfig.json')
        //console.log('this.data:', this.data)
        let refSourceid = this.$route.params.sourceId
        // no use
        // let refTemplate = this.$route.params.sourceTemplate
        let refObj = this.$route.params.tobo
        // 目前只是 bld & account
        if ((refObj == 'bld' && this.$route.params.bo == 'account') || (refObj == 'bld' && this.$route.params.bo == 'account1')) {
          let returnUri = configData.redirectDomain + '/vue-common/dist/new?idobject=' + refObj + '&ref_sourceid=' + refSourceid
          let toUri = configData.baseURL + configData.url3 + '?from=crm&action=create' + '&return_uri=' + encodeURIComponent(returnUri)
          //console.log('redirect_uri:', returnUri)
          //console.log('to_uri:', toUri)
          window.location.href = toUri
        } else {
          this.$router.push({
            name: 'new',
            query: { idobject: this.$route.params.bo, newflag: 'refnew', reBackflag: this.rebackobject }
          })
        }
      } else {
        this.$router.push({
          name: 'new',
          query: { idobject: this.$route.params.bo, newflag: 'refnew', reBackflag: this.rebackobject }
        })
      }
    },
    onSearch () {
      this.list = []
      this.startline = 1
      this.onLoad()
    },
    onCancel () {
      this.sval = ''
      this.list = []
      this.startline = 1
      // this.onLoad()
      this.$router.go(-1)
    },
    reBack (item) {
      // 回写
      // this.$toast(item.id + '-->' + item.title)
      //console.log('refBack: ', item.id, this.fieldref, item[this.fieldref], this.$route.params.field, item)
      this.$store.commit('refReBack', { ref_id: item.id, ref_show: item[this.fieldref], ref_field: this.$route.params.field, ref_item: item })
      this.$router.go(-1)
    }
  },
  activated () {
    if (this.$store.state.ref_show === 'true') {
      this.$store.state.ref_show = ''
      this.$store.state.ref_id = ''
      this.$router.go(-1)
    }
  }
}
</script>

<style>
.fieldgroup_title {
  text-align: left;
  margin: 0px;
  padding: 35px 15px 15px;
  color: rgba(69, 90, 100, 0.6);
  font-weight: normal;
  font-size: 14px;
}
</style>
